package com.thunder.reader.transaction;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import com.thunder.reader.provider.Feed;

import android.content.ContentResolver;
import android.database.Cursor;
import android.text.TextUtils;

public class OpmlGenerator {
    private OutputStream mOutputStream;
    private ContentResolver mContentResolver;
    
    public OpmlGenerator(ContentResolver cr, OutputStream os) {
        mContentResolver = cr;
        mOutputStream = os;
    }
    
    public void generate() throws IOException {
        if (mOutputStream == null) {
            throw new IOException("OutputStream is null, did you initialize this class correctly?");
        }
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(mOutputStream);
            // xml header
            writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            // opml header
            writer.println("<opml version=\"1.0\">");
            writer.println("<head>");
            writer.println("<text/>");
            writer.println("</head>");
            // opml body
            writer.println("<body>");
            // read from database, group by group
            addGroups(writer);
            writer.println("</body>");
            writer.println("</opml>");
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
    
    private void addGroups(PrintWriter writer) throws IOException {
        Cursor cursor = null;
        try {
            cursor = mContentResolver.query(Feed.Group.CONTENT_URI, Feed.Group.PROJECTION, null, null, null);
            if (cursor == null || !cursor.moveToFirst()) {
                return; // fall through to finally clause
            }
            do {
                writer.print("<outline text=\"");
                writer.print(cursor.getString(Feed.Group.DESCRIPTION_INDEX));
                writer.print("\" title=\"");
                writer.print(cursor.getString(Feed.Group.TITLE_INDEX));
                writer.println("\">");
                // add feeds belonging to this group
                final String id = cursor.getString(Feed.Group.ID_INDEX);
                addFeeds(id, writer);
                writer.println("</outline>");
            } while (cursor.moveToNext());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    
    private void addFeeds(String id, PrintWriter writer) throws IOException {
        Cursor cursor = null;
        try {
            cursor = mContentResolver.query(Feed.Source.CONTENT_URI, Feed.Source.PROJECTION, Feed.Source.GROUP_ID +"=?", 
                    new String[]{id}, null);
            if (cursor == null || !cursor.moveToFirst()) {
                return;
            }
            do {
                String xmlUrl = cursor.getString(Feed.Source.FEED_URL_INDEX);
                if (TextUtils.isEmpty(xmlUrl)) {
                    continue;
                }
                writer.print("<outline version=\"RSS\" type=\"rss\" xmlUrl=\"");
                writer.print(xmlUrl);
                String title = cursor.getString(Feed.Source.TITLE_INDEX);
                if (!TextUtils.isEmpty(title)) {
                    writer.print("\" title=\"");
                    writer.print(title);
                }
                final String description = cursor.getString(Feed.Source.DESCRIPTION_INDEX);
                if (!TextUtils.isEmpty(description)) {
                    writer.print("\" description=\"");
                    writer.print(description);
                }
                final String htmlUrl = cursor.getString(Feed.Source.URL_INDEX);
                if (!TextUtils.isEmpty(htmlUrl)) {
                    writer.print("\" htmlUrl=\"");
                    writer.print(htmlUrl);
                }
                final String imageUrl = cursor.getString(Feed.Source.IMAGE_URL_INDEX);
                if (!TextUtils.isEmpty(imageUrl)) {
                    writer.print("\" imageUrl=\"");
                    writer.print(imageUrl);
                }
                writer.println("\"/>");
            } while (cursor.moveToNext());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}